//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    addressList: {},
    items:'',
    id:''
  },
  addAddess: function () {
    wx.navigateTo({
      url: "/pages/address-add/index"
    })
  },
  addSure: function(e){
    var that=this;
    var items=that.data.items;
    var id=that.data.id;
    wx.navigateTo({
      url: '../pay/jiesuan?items=' + items+'&receiverId='+id
    })
    console.log(this.data.id);
  },
  checkboxChange: function (e) {
    var addressList = this.data.addressList, values = e.detail.value;
    for (var i = 0, lenI = addressList.length; i < lenI; ++i) {
      addressList[i].is_default = false;
    }
    var index = parseInt(e.currentTarget.dataset.index) ? parseInt(e.currentTarget.dataset.index) : 0;
    addressList[index].is_default = true;
    this.setData({
      addressList: addressList,
      id: addressList[index].id
    });
  },
  onShow:function(){
    this.onLoad();
  },
  onLoad: function (param) {
    var that = this
    if (param.items){
       that.setData({
         items:param.items
       })
    }
    app.ajaxJson('/receiver/list', {}, function (res) {
      var json = res.data;
      if (json.code != 200) {
        return;
      }
      that.setData({
        addressList: json.data
      });

      var addressList = that.data.addressList;
      for (var i = 0, lenI = addressList.length; i < lenI; ++i) {
        if(addressList[i].is_default){
          that.setData({
            id: addressList[i].id
          });
        }
      }
    });

  }

})
